package com.liu.student.service.impl;

import com.liu.student.entity.Enrollment;
import com.liu.student.mapper.EnrollmentMapper;
import com.liu.student.mapper.CourseMapper;
import com.liu.student.service.EnrollmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class EnrollmentServiceImpl implements EnrollmentService {

    @Autowired
    private EnrollmentMapper enrollmentMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public List<Enrollment> findAll() {
        return enrollmentMapper.findAll();
    }

    @Override
    public List<Enrollment> findByStudentId(Long studentId) {
        return enrollmentMapper.findByStudentId(studentId);
    }

    @Override
    public List<Enrollment> findByCourseId(Long courseId) {
        return enrollmentMapper.findByCourseId(courseId);
    }

    @Override
    public Enrollment findById(Long id) {
        return enrollmentMapper.findById(id);
    }

    @Override
    public void save(Enrollment enrollment) {
        enrollmentMapper.insert(enrollment);
    }

    @Override
    public void update(Enrollment enrollment) {
        enrollmentMapper.update(enrollment);
    }

    @Override
    public void deleteById(Long id) {
        enrollmentMapper.deleteById(id);
    }

    @Override
    public void enrollCourse(Long studentId, Long courseId) throws Exception {
        // 检查是否已选课
        if (isEnrolled(studentId, courseId)) {
            throw new Exception("您已经选择了这门课程");
        }

        // 检查时间冲突
        if (hasTimeConflict(studentId, courseId)) {
            throw new Exception("选课时间冲突，请选择其他课程");
        }

        // 尝试增加课程已选人数（乐观锁）
        int updated = courseMapper.tryIncreaseSelected(courseId);
        if (updated == 0) {
            throw new Exception("课程已满，无法选课");
        }

        // 创建选课记录
        Enrollment enrollment = new Enrollment();
        enrollment.setStudentId(studentId);
        enrollment.setCourseId(courseId);
        enrollment.setStatus("ENROLLED");
        enrollmentMapper.insert(enrollment);
    }

    @Override
    public void dropCourse(Long studentId, Long courseId) throws Exception {
        // 检查是否已选课
        Enrollment enrollment = enrollmentMapper.findByStudentAndCourse(studentId, courseId);
        if (enrollment == null || !"ENROLLED".equals(enrollment.getStatus())) {
            throw new Exception("您没有选择这门课程");
        }

        // 更新选课状态为已退课
        enrollment.setStatus("DROPPED");
        enrollmentMapper.update(enrollment);

        // 减少课程已选人数
        courseMapper.tryDecreaseSelected(courseId);
    }

    @Override
    public boolean isEnrolled(Long studentId, Long courseId) {
        Enrollment enrollment = enrollmentMapper.findByStudentAndCourse(studentId, courseId);
        return enrollment != null && "ENROLLED".equals(enrollment.getStatus());
    }

    @Override
    public boolean hasTimeConflict(Long studentId, Long courseId) {
        // 这里需要实现时间冲突检查逻辑
        // 暂时返回false，表示没有冲突
        // 实际实现需要查询学生已选课程的时间安排，与新课程时间进行比较
        return false;
    }
}